//
//  WDGrabOrderPersonCarInfoView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/7.
//

#import "WDGrabOrderPersonCarInfoView.h"
#import "WDArrivalServiceItemModel.h"

@interface WDGrabOrderPersonCarInfoView ()
@property (nonatomic, strong) WDArrivalServiceItemModel *model;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) CALayer *horLine;

@property (nonatomic, strong) CALayer *topLine;

/// 车牌号
@property (nonatomic, strong) UILabel *plateNumberLabel;

/// vin码后八位
@property (nonatomic, strong) UILabel *vinLabel;

/// 更新时间
@property (nonatomic, strong) UILabel *updateTimeLabel;

/// 图片
@property (nonatomic, strong) UIImageView *thumalImgView;

/// 车系
@property (nonatomic, strong) UILabel *vehicleSalesLabel;

/// 车型
@property (nonatomic, strong) UILabel *modelNameLabel;

/// 工单号
@property (nonatomic, strong) UILabel *workOrderCodeLabel;

/// 预约时间
@property (nonatomic, strong) UILabel *appointDatetimeLabel;

/// 距离服务站
@property (nonatomic, strong) UILabel *distanceLabel;

@property (nonatomic, strong) CALayer *imgBottomLine;

@property (nonatomic, strong) UIImageView *locImgView;

/// 地址
@property (nonatomic, strong) UILabel *adsLabel;

@end

@implementation WDGrabOrderPersonCarInfoView

- (void)initWithSubView {
    [super initWithSubView];
    
    self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    
    [self initWithTopLine];
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(24, 14, 200, 20);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"车辆基本信息";
        }];
        
        CGRect lframe = CGRectMake(20, 0, 2, 10);
        _horLine = [CALayer drawLine:self frame:lframe lineColor:kThemeColor];
        _horLine.centerY = self.titleLabel.centerY;
    }
}

- (void)initWithTopLine {
    if (!_topLine) {
        CGRect frame = CGRectMake(20, 72, self.width - 20 * 2, 1);
        _topLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#EEEEEE"]];
    }
}

/// 车牌号
- (void)initWithPlateNumberLabel {
    if (!_plateNumberLabel) {
        NSString *str = @"皖A12345645";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [str widthForFont:font];
        CGRect frame = CGRectMake(20, self.topLine.top - 16 - 9, width, 16);
        _plateNumberLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.plateNumber);
        float width = [str widthForFont:_plateNumberLabel.font] + 2;
        self.plateNumberLabel.text = str;
        self.plateNumberLabel.width = width;
    }
}

- (void)initWithVinLabel {
    if (!_vinLabel) {
        CGRect frame = _plateNumberLabel.frame;
        frame.size.width = frame.size.width + 10;
        frame.origin.x = _plateNumberLabel.right + 5;
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        _vinLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.vehicleVin);
        if (self.model.vehicleVin && self.model.vehicleVin.length >= 8) {
            str = [self.model.vehicleVin substringFromIndex:self.model.vehicleVin.length - 8];
        }
        float width = [str widthForFont:_vinLabel.font] + 2;
        self.vinLabel.width = width;
        self.vinLabel.left = self.plateNumberLabel.right + 5;
        self.vinLabel.text = str;
    }
}

/// 更新时间
- (void)initWithUpdateTimeLabel {
    if (!_updateTimeLabel) {
        NSString *str = @"更新：2024-12-12 23:34:54";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [str widthForFont:font] + 2;
        CGRect frame = self.plateNumberLabel.frame;
        frame.size.width = width;
        frame.origin.x = self.width - 20 - width;
        _updateTimeLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textAlignment = NSTextAlignmentRight;
        }];
    }
    if (self.model) {
        NSString *str = [NSString stringWithFormat:@"更新：%@", objectNull(self.model.updateTime)];
        float width = [str widthForFont:_updateTimeLabel.font] + 2;
        self.updateTimeLabel.width = width;
        self.updateTimeLabel.left = self.width - 20 - width;
        self.updateTimeLabel.text = str;
    }
}

- (void)initWithThumalImgView {
    if (!_thumalImgView) {
        CGRect frame = CGRectMake(20, self.topLine.bottom + 20, 98, 76);
        _thumalImgView = [UIImageView createWithImageView:self frame:frame defaultImage:nil propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
    if (self.model) {
        [_thumalImgView sd_setImageWithURL:[NSURL URLWithString:self.model.imgPath] placeholderImage:[UIImage imageNamed:@"ic_default_veh"]];
    }
}

/// 车系
- (void)initWithVehicleSalesLabel {
    if (!_vehicleSalesLabel) {
        CGRect frame = CGRectMake(self.thumalImgView.right + 12, self.topLine.bottom + 8, self.width - self.thumalImgView.right - 12 - 10, 20);
        _vehicleSalesLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
//            label.backgroundColor = [UIColor orangeColor];
        }];
    }
    if (self.model) {
        self.vehicleSalesLabel.text = objectNull(self.model.vehicleSalesValue);
    }
}

/// 车型
- (void)initWithModelNameLabel {
    if (!_modelNameLabel) {
        CGRect frame = self.vehicleSalesLabel.frame;
        frame.size.height = 18;
        frame.origin.y = self.vehicleSalesLabel.bottom + 1;
        frame.size.width = self.width - self.thumalImgView.right - 20 - 12;
        _modelNameLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#19C46E"];
//            label.backgroundColor = [UIColor orangeColor];
        }];
    }
    if (self.model) {
        self.modelNameLabel.text = objectNull(self.model.modelName);
    }
}



/// 工单号
- (void)initWithWorkOrderCodeLabel {
    if (!_workOrderCodeLabel) {
//        NSString *str = @"工单号：ABCDQWEQ";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
//        float width = [str widthForFont:font] + 5;
        CGRect frame = self.modelNameLabel.frame;
        frame.size.height = 18;
        frame.origin.y = self.modelNameLabel.bottom + 1;
        _workOrderCodeLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#888888"];
        }];
    }
    if (self.model) {
        self.workOrderCodeLabel.text = [NSString stringWithFormat:@"工单号：%@", self.model.taskNo];
    }
}

/// 预约时间
- (void)initWithAppointDatetimeLabel {
    if (!_appointDatetimeLabel) {
//        NSString *str = @"预约日期：2024-05-12 12:34";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        CGRect frame = self.workOrderCodeLabel.frame;
        frame.origin.y = self.workOrderCodeLabel.bottom + 2;
        _appointDatetimeLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#888888"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.appointmentTime);
        if ([str isEqualToString:@""]) {
            str = objectNull(self.model.createTime);
        }
        self.appointDatetimeLabel.text = [NSString stringWithFormat:@"预约日期：%@", str];
    }
}

/// 距离服务站
- (void)initWithDistanceLabel {
    if (!_distanceLabel) {
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        CGRect frame = self.appointDatetimeLabel.frame;
        frame.origin.y = self.appointDatetimeLabel.bottom + 2;
        _distanceLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#888888"];
        }];
    }
    if (self.model) {
        NSString *distance = @"";
        if (self.model.rescueDistance && [self.model.rescueDistance integerValue] > 0) {
            distance = [NSString stringWithFormat:@"%zdm", [self.model.rescueDistance integerValue]];
        }
        self.distanceLabel.text = [NSString stringWithFormat:@"距离服务站：%@", distance];
    }
}

- (void)initWithImgBottomLine {
    if (!_imgBottomLine) {
        CGRect frame = CGRectMake(self.thumalImgView.left, self.thumalImgView.bottom + 22, self.width - self.thumalImgView.left * 2, 1);
        _imgBottomLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#EEEEEE"]];
    }
}

- (void)initWithLocImgView {
    if (!_locImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_main_loc_blue"];
        CGRect frame = CGRectMake(20, self.imgBottomLine.bottom + (self.height - self.imgBottomLine.bottom - image.size.height) * 0.5, image.size.width, image.size.height);
        _locImgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
        }];
    }
}

- (void)initWithAdsLabel {
    if (!_adsLabel) {
        CGRect frame = CGRectMake(self.locImgView.right + 6, 0, self.imgBottomLine.width - self.locImgView.width - 6, 20);
        _adsLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
//            label.backgroundColor = [UIColor orangeColor];
        }];
    }
    if (self.model) {
        self.adsLabel.centerY = self.locImgView.centerY;
        NSString *str = @"";
        if (![objectNull(self.model.rescueAddress) isEqualToString:@""]) {
            str = objectNull(self.model.rescueAddress);
        }
        if (self.model.rescueDistance && [self.model.rescueDistance integerValue] > 0) {
            str = [NSString stringWithFormat:@"%@ %zd", str, [self.model.rescueDistance integerValue]];
        }
        _adsLabel.text = str;
    }
}

- (void)updateWithViewData:(id)data {
    self.model = data;
    
    [self initWithTitleLabel];
    
    [self initWithPlateNumberLabel];
    
    [self initWithVinLabel];

    [self initWithUpdateTimeLabel];
    
    [self initWithThumalImgView];
    
    /// 车系
    [self initWithVehicleSalesLabel];

    /// 车型
    [self initWithModelNameLabel];

    /// 工单号
    [self initWithWorkOrderCodeLabel];
//
    /// 预约时间
    [self initWithAppointDatetimeLabel];
    
    // 距离服务站
    [self initWithDistanceLabel];
    
    [self initWithImgBottomLine];
    
    [self initWithLocImgView];
    
    [self initWithAdsLabel];
}

@end
